/*
package com.jason.login.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

public class MyDataSource implements DataSource {

    private DruidDataSource druidDataSource = new DruidDataSource();

    private Boolean init = Boolean.FALSE;

    private String url;

    private String driver;

    private String username;

    private String password;

    @Override
    public Connection getConnection() throws SQLException {
        ZkClient zkClient = new ZkClient("127.0.0.1:2181");
        if (!init)
            init(zkClient);

        //注册监听
        zkClient.subscribeDataChanges("/datasource", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println("数据发生改变，重新获取数据库");
                druidDataSource = new DruidDataSource();
                init(zkClient);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {

            }
        });

        return druidDataSource.getConnection();
    }

    @Override
    public Connection getConnection(String s, String s1) throws SQLException {
        return druidDataSource.getConnection(s, s1);
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return druidDataSource.getLogWriter();
    }

    @Override
    public void setLogWriter(PrintWriter printWriter) throws SQLException {
        druidDataSource.setLogWriter(printWriter);
    }

    @Override
    public void setLoginTimeout(int i) throws SQLException {
        druidDataSource.setLoginTimeout(i);
    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return druidDataSource.getParentLogger();
    }

    @Override
    public <T> T unwrap(Class<T> aClass) throws SQLException {
        return druidDataSource.unwrap(aClass);
    }

    @Override
    public boolean isWrapperFor(Class<?> aClass) throws SQLException {
        return druidDataSource.isWrapperFor(aClass);
    }

    private void init (ZkClient zkClient) {
        init = Boolean.TRUE;
        System.out.println("创建连接池");
        String datasource = zkClient.readData("/datasource");
        System.out.println("数据库的配置信息:"  + datasource);
        String[] strs = datasource.split("#");
        url = strs[0];
        driver = strs[1];
        username = strs[2];
        password = strs[3];

        druidDataSource.setUrl(url);
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setConnectionErrorRetryAttempts(0);
        druidDataSource.setBreakAfterAcquireFailure(true);
    }
}
*/
